import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {HttpClient} from '@angular/common/http';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-user-add',
  templateUrl: './user-add.component.html',
  styleUrls: ['./user-add.component.css']
})

export class UserAddComponent implements OnInit {
  userAddForm: FormGroup;
  url = 'http://127.0.0.1:8080/lx/';
  depList = [];
  id: number;
  name;
  constructor(private fb: FormBuilder,
              private http: HttpClient,
              private activatedRoute: ActivatedRoute,
              ) {
  }

  ngOnInit() {
    this.userAddForm = this.fb.group({
      perName: ['', [Validators.required]],
      account: ['', [Validators.required]],
      citID: ['', [Validators.required]],
      mobile: ['', [Validators.required]],
      email: ['', [Validators.required]],
      sexCode: [1, [Validators.required]],
      role: [1, [Validators.required]],
      dep: ['', [Validators.required]]
    });
    this.getDepList();
    this.id = this.activatedRoute.snapshot.queryParams['id'];
    this.name = this.activatedRoute.snapshot.queryParams['name'];
    /*this.activatedRoute.queryParams.subscribe(v=>{
      this.id = v['id'];
      this.name = v['name']
    })*/
    if (this.id) {
      this.editme(Number(this.id));
    }
  }

  getFormControl(data) {
    return this.userAddForm.controls[data];
  }

  // 编辑
  editme(id) {
    this.http.post(this.url + 'getUserById', id).subscribe(v => {
      console.log(v);
      if (v['code'] === 0) {
        // const arr = ['account','role','sexCode','citID','mobile','email','perName','department'];
        // arr.map(d=>{
        //   this.getFormControl(d).setValue(v['data'][d]);
        // })
        ['account', 'role', 'sexCode', 'citID', 'mobile', 'email', 'perName'].map(d => {
          this.getFormControl(d).setValue(v['data'][d]);
        })
        /*this.getFormControl('account').setValue(v['data']['account']);
        this.getFormControl('perName').setValue(v['data']['perName']);
        this.getFormControl('citID').setValue(v['data']['citID']);
        this.getFormControl('mobile').setValue(v['data']['mobile']);
        this.getFormControl('email').setValue(v['data']['email']);
        this.getFormControl('sexCode').setValue(v['data']['sexCode']);
        this.getFormControl('role').setValue(v['data']['role']);
        */
        if (v['data']['department']) {
          this.getFormControl('dep').setValue(v['data']['department']['id']);
        }
      }
    })
  }

  // 获取部门下拉数据
  getDepList() {
    this.http.get(this.url + 'getDepartmentSelect').subscribe(v => {
      if (v['code'] === 0) {
        this.depList = v['data'];
        // console.log(v);
      }
    })
  }

  // 保存
  saveme(allValue) {
    // console.log(allValue);
    this.http.put(this.url + 'saveUser', {
      id: this.id ? this.id : null,
      account: allValue['account'],
      perName: allValue['perName'],
      email: allValue['email'],
      mobile: allValue['mobile'],
      sexCode: allValue['sexCode'],
      citID: allValue['citID'],
      role: allValue['role'],
      department: allValue['dep'] ? {
        id: allValue['dep']
      } : null
    }).subscribe(v => {
      if (v['code'] === 0) {
        alert('保存成功!');
        this.ngOnInit();
      } else {
        alert('保存失败!');
      }
    })
  }
}
